//
//  AFNetworkingOnLineViewController.m
//  LianXi7.11
//
//  Created by apple on 2017/7/12.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "AFNetworkingOnLineViewController.h"
#import <AFNetworking.h>
@interface AFNetworkingOnLineViewController ()<NSURLSessionDownloadDelegate>

@property (nonatomic, strong) AFURLSessionManager *manager;

@property (nonatomic, strong) NSURLSessionDownloadTask *task;

@property (nonatomic, strong) UIProgressView *progress;
@property (nonatomic, strong) UILabel *progressLabel;

@property (nonatomic, strong) UIButton *btn;

@end

@implementation AFNetworkingOnLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];

    
    self.progress = [[UIProgressView alloc] initWithFrame:CGRectMake(30, 100, 200, 10)];
    
    self.progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 70, 100, 20)];
    
    self.progressLabel.textColor = [UIColor blueColor];
    
    [self.view addSubview:_progressLabel];
    
    [self.view addSubview:_progress];
    
    self.btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 50, 50)];
    self.btn.backgroundColor = [UIColor blueColor];
    [self.btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    
    NSString *str = @"http://dldir1.qq.com/qqfile/QQforMac/QQ_V5.4.0.dmg";
    
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    self.manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    self.task = [self.manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        
        //获取主线程，缺了这个可不行
        NSOperationQueue *que = [NSOperationQueue mainQueue];
        
        [que addOperationWithBlock:^{
            
            self.progress.progress = 1.0 * downloadProgress.completedUnitCount/downloadProgress.totalUnitCount;
            
            self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%",100.0 *downloadProgress.completedUnitCount/downloadProgress.totalUnitCount];
        }];
        
        
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        
//        NSURL *path = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
//        return [path URLByAppendingPathComponent:@"QQ_V5.4.0.dmg"];
        
        NSURL *path = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        
        return [path URLByAppendingPathComponent:@"QQ.dmg"];
        
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        
        NSLog(@"File downloaded to: %@", filePath);
    }];
    
//    [self.task resume];
    
    [self createBtnBack];
}

- (void)btnClick:(UIButton *)sender{

    sender.selected = !sender.isSelected;
    
    if (sender.selected) {
    
        [self.task resume];
    
    }else{
    
        [self.task suspend];
//        self.task = nil;
    }
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{}


- (void)createBtnBack{
    
    UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(30, 440, 50, 50)];
    
    [btnBack setTitle:@"BACK" forState:UIControlStateNormal];
    [btnBack setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    btnBack.backgroundColor = [UIColor greenColor];
    
    [btnBack addTarget:self action:@selector(btnBack) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnBack];
    
}

- (void)btnBack{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
